package com.academic_system.controller;

import com.academic_system.model.Course;
import com.academic_system.model.MarkCondition;
import com.academic_system.model.MarkCount;
import com.academic_system.service.CourseService;
import com.academic_system.service.MarkService;
import com.academic_system.service.StudentService;
import com.academic_system.service.TermService;
import com.academic_system.util.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 20155884 王晓阳
 * @className : MarkController
 * @Description
 **/
@Controller
@RequestMapping("/mark")
@ResponseBody
@CrossOrigin(origins = "*")
public class MarkController {
    @Autowired
    private MarkService markService;
    @Autowired
    private TermService termService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private StudentService studentService;

    //条件查询成绩
    @RequestMapping(value = "/selectMark", method = RequestMethod.GET)
    public Message selectMark(MarkCondition markCondition) {
        System.out.println("courseid:"+markCondition.getCourseId()+"  课程类别:" + markCondition.getCourseType() + "  课程名称：" + markCondition.getCourseName() + "  学年:" + markCondition.getTermYear() + "  学期:" + markCondition.getTermNum());
        System.out.println(markCondition.toString());
        // Integer.parseInt(markCondition.getCourseId());
        return markService.selectMark(markCondition);
    }

    //查找所有课程类别--下拉列表
    @RequestMapping(value = "/selectCourseType", method = RequestMethod.GET)
    public Message selectCourseType() {
        System.out.println("-markcontroller-selectCourseType-");
        return courseService.queryAllCouseDistict();
    }

    //查找不及格成绩
    @RequestMapping(value = "/selectFailGrade", method = RequestMethod.GET)
    public Message selectFailGrade(MarkCondition markCondition) {
        System.out.println("-markcontroller-selectFailGrade-");
        System.out.println(markCondition.toString());
        return markService.selectFallGrade(markCondition);
    }

    //通过课程类型查课程名称--下拉列表
    @RequestMapping(value = "/selectCourseNameByCoursetype", method = RequestMethod.GET)
    public Message selectCourseNameByCoursetype(Course course) {
        System.out.println("-markcontroller-selectCourseNameByCoursetype-" + course);
        return courseService.queryCousecourseType(course);
    }

    //添加成绩
    @RequestMapping(value = "/addMark", method = RequestMethod.GET)
    public Message addMark(MarkCondition markCondition) {
        System.out.println("---"+markCondition.toString());
        System.out.println("-markcontroller-addMark-" + markCondition.getMark() + "  学期学年：" + markCondition.getTermYear() + markCondition.getTermNum() + "  学号：" + markCondition.getStuNum() + "  姓名：" + markCondition.getStuName());
        //通过学年学期查询学期id
        int termid = termService.queryTermId(markCondition.getTermYear(), markCondition.getTermNum()).getData().getTermId();
        markCondition.setTermId(termid);
        //绩点
        Double gradePoint =  GradePoint(markCondition.getMark());
        markCondition.setMarkGradepoint(gradePoint);
        //通过课程名称和课程类型查找课程id
        int courseid = courseService.queryCouseId(markCondition.getCourseType(), markCondition.getCourseName()).getData().getCourseId();
        markCondition.setCourseId(courseid);
        //通过学生学号查找学生id
        int stuInfoId = studentService.queryStuIdByStuNum(markCondition.getStuNum()).getData().getStuInfoId();
        markCondition.setStuInfoId(stuInfoId);
        System.out.println("*************course**************"+markCondition.getCourseId()+"******term**"+markCondition.getTermId());
        markService.addMark(markCondition);
        markService.queryNoScoreStudent(markCondition);

        return Message.success().add("100","成功");
    }
   /* //查找没有录过成绩的学生
    @RequestMapping(value = "/queryNoScoreStudent", method = RequestMethod.GET)
    public Msg queryNoScoreStudent(MarkCondition markCondition) {
        return markService.queryNoScoreStudent(markCondition);
    }*/
    //查找所有学生
    @RequestMapping(value = "/queryAllStudent", method = RequestMethod.GET)
    public Message queryAllStudent(MarkCondition markCondition) {
        return studentService.queryStudent();
    }

    //查询根据学年学期，课程id...查询课程--成绩管理页面
    @RequestMapping(value = "/findCourseTerm", method = RequestMethod.GET)
    public Message findCourseTerm(MarkCondition markCondition) {
        System.out.println("课程类别xdh+:" + markCondition.getCourseType() + "  课程名称：" + markCondition.getCourseName() + "  学年:" + markCondition.getTermYear() + "  学期:" + markCondition.getTermNum());

        return markService.findCourseTerm(markCondition);
    }

    //更新学生成绩
    @RequestMapping(value = "/updateMark", method = RequestMethod.GET)
    public Message updateMark(MarkCondition markCondition) {
        System.out.println("-markcontroller-updateMark-");
        System.out.println("成绩："+markCondition.getMark()+"  课程类别:" + markCondition.getCourseType() + "  课程名称：" + markCondition.getCourseName() + "  学年:" + markCondition.getTermYear() + "  学期:" + markCondition.getTermNum());
       //修改绩点
        Double gradePoint =  GradePoint(markCondition.getMark());
        System.out.println("修改后绩点:"+gradePoint);
        markCondition.setMarkGradepoint(gradePoint);
        markCondition.setMark(markCondition.getMark());
        return markService.updateMarkByMarkId(markCondition);
    }
    //查找各分数段的学生人数
    @RequestMapping(value = "/selectCount", method = RequestMethod.GET)
    public Message selectCount(MarkCount markCount) {
        System.out.println("-markcontroller-selectCount-"+markCount.toString());
        return markService.selectMarkCount(markCount);
    }
    //学业警告，1.查询每个学生某个学期所修学分 2.查询某学期本应修的全部学分
    /*@RequestMapping(value = "/selectWarning", method = RequestMethod.GET)
    public Message selectWarning(MarkCondition markCondition) {
        System.out.println("warning:"+markCondition.toString());
        //通过学年学期查询学期id
        int termid = termService.queryTermId(markCondition.getTermYear(), markCondition.getTermNum()).getData().getTermId();
        markCondition.setTermId(termid);
        return markService.findStuCredithour1(markCondition);
    }*/
    //绩点
    public Double GradePoint(Integer mark){
        Double gradePoint = 0.0;
        if (mark < 60) {
            gradePoint=0.0;
            System.out.println("小于60 绩点是:" + gradePoint);
        }
        if (mark >= 60 && mark < 65) {
            gradePoint=1.0;
            System.out.println("60<x<65 绩点是:" + gradePoint);
        }
        if (mark >= 65 &&mark < 70) {
            gradePoint=1.5;
            System.out.println("65<x<70 绩点是:" + gradePoint);
        }
        if (mark >= 70 && mark < 75) {
            gradePoint=2.0;
            System.out.println("70<x<75 绩点是:" + gradePoint);
        }
        if (mark >= 75 &&mark< 80) {
            gradePoint=2.5;
            System.out.println("75<x<80 绩点是:" + gradePoint);
        }
        if (mark >= 80 && mark< 85) {
            gradePoint=3.0;
            System.out.println("80<x<85 绩点是:" + gradePoint);
        }
        if (mark >= 85 &&mark< 90) {
            gradePoint=3.5;
            System.out.println("85<x<90 绩点是:" + gradePoint);
        }
        if (mark >= 90 &&mark < 95) {
            gradePoint=4.0;
            System.out.println("80<x<85 绩点是:" + gradePoint);
        }
        if (mark >= 95 &&mark < 100) {
            gradePoint=4.5;
            System.out.println("95<x<100 绩点是:" + gradePoint);
        }
        if (mark == 100) {
            gradePoint=5.0;
            System.out.println("100 绩点是:" + gradePoint);
        }
        return gradePoint;
    }
}
